package com.wr.service.com.wr.serializer;

import com.alibaba.fastjson.JSON;
import com.wr.service.com.wr.request.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author 王瑞
 * @description: TODO
 * @date 2020/3/26  23:53
 */
public class RpcDecoder extends MessageToMessageDecoder<ByteBuf> {

    private Class<?> clazz;

    private Serializer serializer;

    public RpcDecoder(Class<?> clazz, Serializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }


    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) throws Exception {
        int dataLength = byteBuf.readInt();
        byte[] body = new byte[dataLength];  //  嗯，这时候，我们读到的长度，满足我们的要求了，把传送过来的数据，取出来吧~~
        byteBuf.readBytes(body);  //
        Object o = serializer.deserialize(RpcRequest.class,body);  //将byte数据转化为我们需要的对象。伪代码，用什么序列化，自行选择
        out.add(o);
    }
}
